﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;

namespace ViewMaker.Core.Controls
{
    /// <summary>
    /// タブコントロールViewControl
    /// </summary>
    public class TabControlViewControl : ControlViewControl
    {
        public new class Properties : ControlViewControl.Properties
        {
            public const string TabStripPlacement = "TabStripPlacement";
        }

        /// <summary>
        /// タブ ヘッダーの配置方法
        /// </summary>
        [View(ViewControlType.ComboBox)]
        [ViewProperty(ComboBoxViewControl.Properties.ItemsSource, "DockList")]
        public LayoutDock? TabStripPlacement { get; set; }

        [XmlIgnore]
        [Browsable(false)]
        public override bool CanAddChild { get { return true; } }

        [XmlIgnore]
        [Browsable(false)]
        public override bool CanOverrideWidth { get { return false; } }

        [XmlIgnore]
        [Browsable(false)]
        public override bool CanOverrideHeight { get { return false; } }

        #region ShouldSerialize

        public override bool ShouldSerializeVerticalAlignment() { return true; }

        #endregion

        public TabControlViewControl()
        {
            ControlType = ViewControlType.TabControl;
            IsPanel = true;
            StreachAlign();
        }

        public override List<XamlNode> CreateXaml(XamlNode parent)
        {
#if !SILVERLIGHT
            var node = parent.CreateElement("TabControl");
            var items = node.AddElement("TabControl.Items");
#else
            var node = parent.CreateElement("sdk", "http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk", "TabControl");
            var items = node.CreateElement("sdk", "http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk", "TabControl.Items");
            node.AppendChild(items);

#endif
            foreach (var child in Item.Children)
            {
#if !SILVERLIGHT
                var item = items.AddElement("TabItem");
#else
                var item = items.CreateElement("sdk", "http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk", "TabItem");
                items.AppendChild(item);
#endif
                item.SetAttribute("Header", child.Caption);
                if (child.Element.IsPanel)
                {

                    foreach (var childItem in child.CreateXaml(item))
                    {
                        if (child.Control.VerticalAlignment.HasValue) childItem.SetAttribute("VerticalAlignment", child.Control.VerticalAlignment.ToString());
                        if (child.Control.HorizontalAlignment.HasValue)
                        {
                            childItem.SetAttribute("HorizontalAlignment", child.Control.HorizontalAlignment.ToString());
                        }
                        else if (child.Control.Width.HasValue)
                        {
                            childItem.SetAttribute("HorizontalAlignment", LayoutHorizontalAlignment.Left.ToString());
                        }
                        item.AppendChild(childItem);
                    }
                }
                else
                {
                    var panel = item.AddElement("StackPanel");
                    foreach (var childItem in child.CreateXaml(panel))
                    {
                        panel.AppendChild(childItem);
                    }
                }
            }
            SetBindingAttribute(node, "DataContext", Item.BindPath, true);
            if (TabStripPlacement.HasValue) node.SetAttribute("TabStripPlacement", TabStripPlacement.ToString());
            ApplyBaseProperty(node);
            var list = new List<XamlNode>();
            list.Add(node);
            return list;
        }


        ////public override int? GetItemWidth(ViewControl ctrl)
        ////{
        ////    if (this.Item.Parent != null)
        ////    {
        ////        var provider = this.Item.Parent.Control as IItemPropProvider;
        ////        if (provider != null) return provider.GetItemWidth(ctrl);
        ////    }
        ////    return null;
        ////}

        ////public override int? GetItemHeight(ViewControl ctrl)
        ////{
        ////    if (this.Item.Parent != null)
        ////    {
        ////        var provider = this.Item.Parent.Control as IItemPropProvider;
        ////        if (provider != null) return provider.GetItemHeight(ctrl);
        ////    }
        ////    return null;
        ////}


    }

}
